package org.ediba.siri.core.action.data;

import java.util.Collection;
import java.util.Map;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.action.data.ActionUsedDao;
import org.ediba.siri.core.action.domain.ActionUsed;
import org.ediba.siri.core.common.data.HibernateDomainDao;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;

public class HibernateActionUsedDao extends HibernateDomainDao<ActionUsed, Integer> implements ActionUsedDao {
	
	public HibernateActionUsedDao(){super(ActionUsed.class);}
	
	protected DetachedCriteria buildCriteria(Collection<Map<Filter,Object>> filters){
		
		DetachedCriteria detachedCriteria = this.buildCriteria();
		Disjunction or = Restrictions.disjunction();
		Conjunction and;
		
		for (Map<Filter, Object> map : filters) {
			and = Restrictions.conjunction();
			for (Map.Entry<Filter,Object> entry: map.entrySet())
				switch (entry.getKey()) {
					case ID:	and.add(Restrictions.idEq(entry.getValue()));						
					break;
					case IMAGE:	if (entry.getValue() == null)
									and.add(Restrictions.isNull(entry.getKey().getProperty()));
								else 
									and.add(Restrictions.eq(entry.getKey().getPropertyId(), entry.getValue())); 
					break;
				};
			or.add(and);
		}

		detachedCriteria.add(or);
		
		return detachedCriteria;
		
	}
	
}
